package com.ruoyi.system.service.impl;

import java.lang.invoke.LambdaConversionException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingRoomOrderMapper;
import com.ruoyi.system.domain.MeetingRoomOrder;
import com.ruoyi.system.service.IMeetingRoomOrderService;

/**
 * 会议室预约记录Service业务层处理
 *
 * @author hehonglin
 * @date 2022-04-09
 */
@Service
public class MeetingRoomOrderServiceImpl implements IMeetingRoomOrderService {
    @Autowired
    private MeetingRoomOrderMapper meetingRoomOrderMapper;

    /**
     * 查询会议室预约记录
     *
     * @param id 会议室预约记录主键
     * @return 会议室预约记录
     */
    @Override
    public MeetingRoomOrder selectMeetingRoomOrderById(String id) {
        return meetingRoomOrderMapper.selectMeetingRoomOrderById(id);
    }

    /**
     * 查询会议室预约记录列表
     *
     * @param meetingRoomOrder 会议室预约记录
     * @return 会议室预约记录
     */
    @Override
    public List<MeetingRoomOrder> selectMeetingRoomOrderList(MeetingRoomOrder meetingRoomOrder) {
        return meetingRoomOrderMapper.selectMeetingRoomOrderList(meetingRoomOrder);
    }

    /**
     * 新增会议室预约记录
     *
     * @param meetingRoomOrder 会议室预约记录
     * @return 结果
     */
    @Override
    public int insertMeetingRoomOrder(MeetingRoomOrder meetingRoomOrder) {
        return meetingRoomOrderMapper.insertMeetingRoomOrder(meetingRoomOrder);
    }

    /**
     * 修改会议室预约记录
     *
     * @param meetingRoomOrder 会议室预约记录
     * @return 结果
     */
    @Override
    public int updateMeetingRoomOrder(MeetingRoomOrder meetingRoomOrder) {
        return meetingRoomOrderMapper.updateMeetingRoomOrder(meetingRoomOrder);
    }

    /**
     * 批量删除会议室预约记录
     *
     * @param ids 需要删除的会议室预约记录主键
     * @return 结果
     */
    @Override
    public int deleteMeetingRoomOrderByIds(Long[] ids) {
        return meetingRoomOrderMapper.deleteMeetingRoomOrderByIds(ids);
    }

    /**
     * 删除会议室预约记录信息
     *
     * @param id 会议室预约记录主键
     * @return 结果
     */
    @Override
    public int deleteMeetingRoomOrderById(Long id) {
        return meetingRoomOrderMapper.deleteMeetingRoomOrderById(id);
    }

    @Override
    public MeetingRoomOrder getOrderByTimeAndId(Long roomId, Date time) {
        if (time == null) {
            time = new Date();
        }
        MeetingRoomOrder meetingRoomOrder = new MeetingRoomOrder();
        meetingRoomOrder.setRoomId(roomId);
        List<MeetingRoomOrder> meetingRoomOrders = selectMeetingRoomOrderList(meetingRoomOrder);
        Date finalTime = time;
        List<MeetingRoomOrder> collect = meetingRoomOrders
                .stream()
                .filter(e -> e.getStartTime().getTime() <= finalTime.getTime() && e.getEndTime().getTime() >= finalTime.getTime())
                .collect(Collectors.toList());
        if (collect.size() > 0) {
            return collect.get(0);
        } else {
            return null;
        }
    }
}
